/**
 * author:Administrator
 * date:2015/7/27
 * desc:TODO
 */
"use strict";

var model = require('./models/user.js');

function UserDao() {
}

var theProto = UserDao.prototype;

theProto.saveOne = function*(json) {
    var query = {uid: json.uid};

    var entity = {
        uid: json.uid,
        roleName: json.roleName
    };

    // in case of duplicate groupName:name
    let result;
    result = yield model.findOne(query);
    if (result && result._doc) {
        delete entity.uid;
        result = yield model.update(query, entity, {upsert: true, new: true});
    } else {
        result = yield new model(entity).save();
    }

    return result;
};

theProto.getById = function*(uid) {
    // aggregate
    var query = {uid: uid};
    let result = yield model.findOne(query); //get all
    return result && result._doc;
};

module.exports = new UserDao();